package com.lab.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.lab.dao.CourseDao;
import com.lab.po.Course;
import com.lab.po.PageBean;
import com.lab.po.Student;
import com.lab.util.MySqlSessionFactory;

public class CourseDaoImpl implements CourseDao{

	
	//分页查询所有课程
	@Override
	public List<Course> queryCourse(Map map) {
		SqlSession session=null;
		List<Course> courseList=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			courseList=session.selectList("CourseXml.queryCourse",map);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return courseList;
	}
	//增加课程
	@Override
	public int addCourse(String cou_name) {
		SqlSession session=null;
		int flag = 0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			flag=session.insert("CourseXml.addCourse",cou_name);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}
	//更新课程
	@Override
	public int updateCourse(Map map) {
		int flag = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			flag= session.update("CourseXml.updateCourse",map);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}
	//删除课程
	@Override
	public int deleteCourse(int cou_id) {
		int flag = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			flag=session.delete("CourseXml.deleteCourse", cou_id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}
	
	//查询课程总数
	@Override
	public int countCourse() {
		SqlSession session=null;
		int count=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			count=session.selectOne("CourseXml.countCourse");
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return count;
	}
	
	//根据id查询课程
	@Override
	public String queryCouName(int cou_id) {
		SqlSession session=null;
		String cou_name=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			cou_name=session.selectOne("CourseXml.queryCouName",cou_id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return cou_name;
	}
	
	//模糊查询课程
	@Override
	public List<Course> queryCourseByCondition(Map map) {
		SqlSession session=null;
		List<Course> courseList=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			courseList=session.selectList("CourseXml.queryCourseByCondition",map);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return courseList;
	}
	
	
	//模糊查询结果数
	@Override
	public int countCouByCondition(Map map) {
		SqlSession session=null;
		int count=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			count=session.selectOne("CourseXml.countCouByCondition",map);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return count;
	}
	
	//查询课程状态
	@Override
	public int status() {
		int i = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.selectOne("CourseXml.status");
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}
	
	//更改选课状态
	@Override
	public int updateStatus(int status) {
		int flag = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			flag= session.update("CourseXml.updateStatus",status);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}
}
